<?php

/**
 * This is the model class for table "{{private_message}}".
 *
 * The followings are the available columns in table '{{private_message}}':
 * @property integer $id
 * @property integer $user_id
 * @property integer $flat_id
 * @property string $name
 * @property string $email
 * @property string $phone
 * @property string $message
 * @property integer $pub_date
 */
class PrivateMessage extends CActiveRecord
{

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return PrivateMessage the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{private_message}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, flat_id, name, email, phone, message, pub_date', 'required'),
			array('id, user_id, flat_id, pub_date, is_send_mail', 'numerical', 'integerOnly'=>true),
			array('name, email, phone', 'length', 'max'=>254),
                        array('email', 'email'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, flat_id, name, email, phone, message, pub_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
        
        protected function beforeSave() {
            if (parent::beforeSave()) {
                
                if($this->is_send_mail == 0){
                    
                    $user = User::model()->findByPk($this->user_id);
                    
                    $rez = Yii::app()->mailer->send(
                                $user->email,
                                'Вам пришло личное сообщение',
                                $this->getMasege()
                            );
                    
                    $this->is_send_mail = 1;
                    $this->save();

                    return true;
                }
            
            }
            else
                return false;
        }

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'Получатель',
                        'flat_id' => 'Квартира',
			'name' => 'Имя',
			'email' => 'Email',
			'phone' => 'Контактный телефон',
			'message' => 'Сообщения',
			'pub_date' => 'Дата отправки',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id);
                $criteria->compare('flat_id',$this->flat_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('message',$this->message,true);
		$criteria->compare('pub_date',$this->pub_date);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        
        private function getMasege(){
            $html =  '<p>Здравствуйте!</p>';
            $html .= '<p>На нашем сайте Вам было отправленно личное сообщение.</p>';
            $html .= '<p>
                            <b>Сообщение:</b><br />'.$this->message.'"
                      </p>';
            $html .= '<p></p>';
            $html .= '<p>
                            <b>Контакты клиента:</b><br />
                            Имя: '.$this->name.'<br />
                            Email: '.$this->email.'<br />
                            Телефон: '.$this->phone.'<br />
                      </p>';
            return $html;
        }
}
